using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class Progress
{
	public class Ability
	{
		public string name;

		public int level;

		public Ability(string name, int level)
		{
			this.name = name;
			this.level = level;
		}

		public void Save(string key)
		{
			PlayerPrefs.SetString(id + "an" + key, name);
			PlayerPrefs.SetInt(id + "al" + key, level);
		}
	}

	public class ImprovementInfo
	{
		public string name;

		public float posX;

		public float posY;

		public float posZ;

		public float rotZ;

		public bool copy;

		public int size;

		public bool hand;

		public ImprovementInfo()
		{
		}

		public ImprovementInfo(string name, float posX, float posY, float posZ, float rotZ, bool copy, int size, bool hand)
		{
			this.name = name;
			this.posX = posX;
			this.posY = posY;
			this.posZ = posZ;
			this.rotZ = rotZ;
			this.copy = copy;
			this.size = size;
			this.hand = hand;
		}

		public ImprovementInfo Load(int i)
		{
			name = PlayerPrefs.GetString(id + "in" + i);
			posX = PlayerPrefs.GetFloat(id + "ix" + i);
			posY = PlayerPrefs.GetFloat(id + "iy" + i);
			posZ = PlayerPrefs.GetFloat(id + "iz" + i);
			rotZ = PlayerPrefs.GetFloat(id + "ir" + i);
			copy = PlayerPrefs.GetInt(id + "ic" + i).ToBool();
			hand = PlayerPrefs.GetInt(id + "ih" + i).ToBool();
			return this;
		}

		public void Save(int i)
		{
			PlayerPrefs.SetString(id + "in" + i, name);
			PlayerPrefs.SetFloat(id + "ix" + i, posX);
			PlayerPrefs.SetFloat(id + "iy" + i, posY);
			if (posZ != 0f)
			{
				PlayerPrefs.SetFloat(id + "iz" + i, posZ);
			}
			else
			{
				PlayerPrefs.DeleteKey(id + "iz" + i);
			}
			if (rotZ != 0f)
			{
				PlayerPrefs.SetFloat(id + "ir" + i, rotZ);
			}
			else
			{
				PlayerPrefs.DeleteKey(id + "ir" + i);
			}
			PlayerPrefs.SetInt(id + "ic" + i, copy.ToInt());
			PlayerPrefs.SetInt(id + "ih" + i, hand.ToInt());
		}
	}

	public static int id = 0;

	public static int points;

	public static int mission;

	public static float[] bodyShape = new float[9];

	public static int foodType;

	public static Ability[] abilities = new Ability[0];

	public static ImprovementInfo[] improvementInfos = new ImprovementInfo[0];

	public static Ability passiveAbility = new Ability(string.Empty, 0);

	public static void ApplyBodyShape(Transform[] skeleton)
	{
		for (int i = 0; i < skeleton.Length; i++)
		{
			float num = bodyShape[i];
			skeleton[i].localScale = new Vector3(num, 1f, num);
		}
	}

	public static Transform ApplyFoodType(Transform head)
	{
		return Object.Instantiate(Info.Body.mouth[foodType], head.GetChild(0)).transform;
	}

	public static void ApplyImprovement(Transform transform, Transform parent, ImprovementInfo info, float divider = 1.6f)
	{
		bool flag = Scene.current == SceneName.CreatureEditor;
		Vector3 localScale = transform.localScale;
		float num = 1f;
		transform.SetParent(parent);
		transform.name = info.name;
		if (flag)
		{
			transform.localRotation = Quaternion.Euler(0f, 0f, info.rotZ);
		}
		else
		{
			transform.localRotation = Quaternion.Euler(270f + info.rotZ, 90f, -90f);
			localScale.z = 0f - localScale.z;
			transform.localScale = localScale;
			num = 0.5f;
		}
		transform.localPosition = new Vector3(0f - info.posX, info.posY, info.posZ * num) / divider;
		if (info.copy || flag)
		{
			Transform transform2 = Object.Instantiate(transform.gameObject).transform;
			transform2.SetParent(parent);
			transform2.name = info.name;
			if (flag)
			{
				transform2.localRotation = Quaternion.Euler(transform.localEulerAngles.x, 0f, 0f - transform.localEulerAngles.z);
			}
			else
			{
				transform2.localRotation = Quaternion.Euler(270f - info.rotZ, 90f, -90f);
			}
			transform2.localPosition = new Vector3(info.posX, info.posY, info.posZ * num) / divider;
			transform2.transform.localScale = new Vector3(0f - localScale.x, localScale.y, localScale.z);
			if (!info.copy)
			{
				transform2.gameObject.SetActive(false);
			}
		}
	}

	public static void Load()
	{
		LoadAbilities();
		LoadBodyShape();
		LoadFoodType();
		LoadImprovements();
		LoadPoints();
		LoadMission();
	}

	private static void LoadAbilities()
	{
		int @int = PlayerPrefs.GetInt(id + "a");
		abilities = new Ability[@int];
		for (int i = 0; i < @int; i++)
		{
			abilities[i] = new Ability(PlayerPrefs.GetString(id + "an" + i), PlayerPrefs.GetInt(id + "al" + i));
		}
		passiveAbility = new Ability(PlayerPrefs.GetString(id + "an"), PlayerPrefs.GetInt(id + "al"));
	}

	private static void LoadBodyShape()
	{
		for (int i = 0; i < bodyShape.Length; i++)
		{
			bodyShape[i] = PlayerPrefs.GetFloat(id + "bs" + i);
		}
	}

	public static Color32[] LoadColors()
	{
		return new Color32[2]
		{
			IntToColor32(PlayerPrefs.GetInt(id + "c0")),
			IntToColor32(PlayerPrefs.GetInt(id + "c1"))
		};
	}

	private static void LoadFoodType()
	{
		foodType = PlayerPrefs.GetInt(id + "ft");
	}

	public static int LoadOtherEditorChanges()
	{
		return PlayerPrefs.GetInt(id + "t");
	}

	public static void LoadImprovements()
	{
		int @int = PlayerPrefs.GetInt(id + "i");
		improvementInfos = new ImprovementInfo[@int];
		for (int i = 0; i < @int; i++)
		{
			improvementInfos[i] = new ImprovementInfo().Load(i);
		}
	}

	public static void LoadPoints()
	{
		points = PlayerPrefs.GetInt(id + "pt");
	}

	public static void LoadMission()
	{
		mission = PlayerPrefs.GetInt(id + "ms");
	}

	public static void SaveAbilities()
	{
		PlayerPrefs.SetInt(id + "a", abilities.Length);
		for (int i = 0; i < abilities.Length; i++)
		{
			abilities[i].Save(i.ToString());
		}
		passiveAbility.Save(string.Empty);
	}

	private static void SaveBodyShape()
	{
		for (int i = 0; i < bodyShape.Length; i++)
		{
			PlayerPrefs.SetFloat(id + "bs" + i, bodyShape[i]);
		}
	}

	public static void SaveColors(Color32[] colors)
	{
		PlayerPrefs.SetInt(id + "c0", Color32ToInt(colors[0]));
		PlayerPrefs.SetInt(id + "c1", Color32ToInt(colors[1]));
	}

	private static void SaveFoodType()
	{
		PlayerPrefs.SetInt(id + "ft", foodType);
	}

	private static void SaveImprovements()
	{
		PlayerPrefs.SetInt(id + "i", improvementInfos.Length);
		for (int i = 0; i < improvementInfos.Length; i++)
		{
			improvementInfos[i].Save(i);
		}
	}

	public static void SaveOtherEditorChanges(int textureId)
	{
		PlayerPrefs.SetInt(id + "t", textureId);
	}

	public static void SavePoints()
	{
		PlayerPrefs.SetInt(id + "pt", points);
	}

	public static void SaveMission()
	{
		PlayerPrefs.SetInt(id + "ms", mission);
	}

	public static void SetBodyShape(Transform[] skeleton)
	{
		for (int i = 0; i < skeleton.Length; i++)
		{
			bodyShape[i] = skeleton[i].localScale.x;
		}
		SaveBodyShape();
	}

	public static void SetFoodType(int foodType)
	{
		Progress.foodType = foodType;
		SaveFoodType();
	}

	private static void SetImprovement(Transform improvement, Transform improvement2, int i, List<Transform> hands)
	{
		if (improvement.gameObject.activeSelf)
		{
			Vector3 localPosition = improvement.localPosition;
			bool activeSelf = improvement2.gameObject.activeSelf;
			bool hand = hands.Contains(improvement);
			improvementInfos[i / 2] = new ImprovementInfo(improvement.name, 0f - localPosition.x, localPosition.y, localPosition.z, improvement.localEulerAngles.z, activeSelf, (int)(improvement.localScale.x * 10f), hand);
		}
	}

	public static bool SetImprovements(Transform improvements)
	{
		List<Transform> list = new List<Transform>();
		List<Transform> list2 = new List<Transform>();
		List<Transform> list3 = new List<Transform>();
		for (int i = 0; i < improvements.childCount; i += 2)
		{
			Transform child = improvements.GetChild(i);
			if (child.GetChild(0).tag == "Leg")
			{
				list2.Add(child);
				list2.Add(improvements.GetChild(i + 1));
				continue;
			}
			list3.Add(child);
			list3.Add(improvements.GetChild(i + 1));
			Hand component = child.GetChild(0).GetComponent<Hand>();
			if ((bool)component && (bool)component.child)
			{
				list.Add(child);
			}
		}
		int count = list2.Count;
		if (list2.Count == 0)
		{
			return false;
		}
		int childCount = improvements.childCount;
		improvementInfos = new ImprovementInfo[childCount / 2];
		int j = 0;
		list2 = list2.OrderBy((Transform o) => o.position.y).ToList();
		for (; j < count; j += 2)
		{
			SetImprovement(list2[j], list2[j + 1], j, list);
		}
		for (int k = 0; k + j < childCount; k += 2)
		{
			SetImprovement(list3[k], list3[k + 1], k + j, list);
		}
		SaveImprovements();
		return true;
	}

	public static void SetPoints(int diffrence)
	{
		points += diffrence;
		SavePoints();
	}

	public static string TexturePath()
	{
		return Info.path + id + ".png";
	}

	private static int Color32ToInt(Color32 color)
	{
		return (color.r << 16) | (color.g << 8) | color.b;
	}

	private static Color32 IntToColor32(int value)
	{
		return new Color32((byte)(value >> 16), (byte)(value >> 8), (byte)value, byte.MaxValue);
	}

	public static bool HasNextSave()
	{
		return PlayerPrefs.HasKey(++id + "a");
	}

	public static void NextMission(int diffrence)
	{
		SetPoints(1);
		mission++;
		SaveMission();
		if ((bool)Library.plug)
		{
			Library.plug.Interstital();
		}
	}
}
